package condominios.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominios.excepcion.DAOExcepcion;
import condominios.modelo.Cuota;
import condominios.modelo.Junta;
import condominios.modelo.Persona;
import condominios.negocio.GestionCuotas;
import condominios.negocio.GestionJuntas;

/**
 * Servlet implementation class PagarCuotaServlet
 */
@WebServlet("/PagarCuotaServlet")
public class PagarCuotaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	//ArrayList<Cuota> cuotas = new ArrayList<Cuota>();
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public PagarCuotaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
				GestionCuotas negocioCuotas = new GestionCuotas();
				try {
					Collection<Cuota> cuotas = negocioCuotas.listarNoPagadas();
					// Guardar en el ambiente de request
					request.setAttribute("CUOTAS_PORPAGAR", cuotas);
					RequestDispatcher rd = request.getRequestDispatcher("pagar-cuotas.jsp");
					rd.forward(request, response);

				} catch (DAOExcepcion e) {
					System.out.println(e.getMessage());
					RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
					rd.forward(request, response);
				}
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession sesion = request.getSession();
		Persona vo = (Persona)sesion.getAttribute("user");
		
		if ( vo == null ){
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
			return;
		}
		
		String idCuota = request.getParameter("txtidcuota");
		String tipoPago = request.getParameter("txttipopago");
		String pagoOK = "NO";
		
		//System.out.println("Valor de idCuota : " + idCuota);
		//System.out.println("Valor de tipoPago : " + tipoPago);
		int idCuotaDel = Integer.parseInt(idCuota);
		
		//actualizar el tipo pago y estado de la cuota
		GestionCuotas negocio = new GestionCuotas();
		try {
			negocio.pagarCuota(idCuotaDel, tipoPago);
			pagoOK = "SI";
			//request.setAttribute("JUNTA_OK", juntaOK);
			sesion.setAttribute("PAGO_OK",pagoOK);
			
			response.sendRedirect(request.getContextPath()	+ "/PagarCuotaServlet");
		} catch (DAOExcepcion e) {
			pagoOK = "NO";
			//request.setAttribute("JUNTA_OK", juntaOK);
			sesion.setAttribute("PAGO_OK",pagoOK);
			
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
		
		// Guardando datos en el scope REQUEST
		/*request.setAttribute("CUOTAS_PORPAGAR", cuotas);
		request.setAttribute("PAGO_OK", pagoOK);

		RequestDispatcher rd = request.getRequestDispatcher("pagar-cuotas.jsp");
		rd.forward(request, response);*/
		
		//this.doGet(request, response);
	}

}
